<?php

// $Id$

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/_sys/tcms_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_item_type_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/menu/tcms_menu_dbc.class.php');


class Menu_Item_Create_WP extends TCMS_Admin_Web_Page_WED_UA {
	public function create_view($name = 'default') {
		$ret = new TCMS_Admin_Page_View($this, $name, 'modules/tangra_cms/pages/_menu/');

		return $ret;
	}


	protected function init_transit_vars() {
		$this->add_transit_var('menu');
	}


	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$type_event = new Web_Event_Simple_Int('type', Tangra_Parameter_Method::GET, 'type');
		$parent_event = new Web_Event_Simple_Int('parent', Tangra_Parameter_Method::GET, 'parent');
		$create_event = new Web_Event_Composite('create');
		$create_event->add_wes($type_event);
		$create_event->add_wes($parent_event);

		$this->wed->add_wed_event_action_pair($create_event, 'create_event', 20);
	}


	protected function default_event() {
		$view = $this->get_view('default');

		$dbc = $this->get_dbc();
		$types = TCMS_Menu_Item_Type_DBC::load_all($dbc);
		$this->export('types', $types);

		$context = $this->get__context();
		if (!$context->exists_in_get('parent')) {
			throw new Tangra_Module_Exception('Missing "parent" parameter');
		}

		$this->export('parent', $context->get_from_get('parent'));

		$menu = new TCMS_Menu_DBC();
		if (!$menu->load_by_id($dbc, $this->get_transit_var('menu'))) {
			throw new Tangra_Module_Exception('Cannot load TCMS_Menu_DBC with id = '.$menu_item->get_menu());
		}
		$this->export('tcms_menu', $menu);

		return $view;
	}


	protected function create_event($params) {
		$type_id = $params['type'];
		$parent = $params['parent'];

		$dbc = $this->get_dbc();
		$type = new TCMS_Menu_Item_Type_DBC();
		if (!$type->load_by_id($dbc, $type_id)) {
			throw new Tangra_Module_Exception('Cannot find TCMS_Menu_Item_Type_DBC with id = ', $type);
		}


		if ($type->get_capture_prefix() == 'manage') {
			$params = array('parent' => $parent);
		} else {
			$params = array($type->get_capture_prefix().'_add_new' => 1, 'parent' => $parent);
		}

		$view = $this->create_redirect_view($type->get_page_edit_path(), $params);

		return $view;
	}
}